##
##  Copyright (C) 1998-2007 J. Andrew McLaughlin
## 
##  Makefile
##

# This makefile builds all the MBRs, boot sectors, and the OS loader.

#ROOT=../..
BOOTBUILDDIR=$(M_DIR_BUILD)/$(M_P_SYSTEM)/boot
LOADERBUILDDIR=$(M_DIR_BUILD)
OBJDIR=$(M_DIR_OBJ)/src/osloader

ifeq ($(RELEASE),)
	RELEASE = $(shell $(M_DIR_UTILS)/release.sh)
endif

DEBUG		= # -DDEBUG # Uncomment to turn on debugging

#include ${ROOT}/Makefile.include

WARN		= -w+orphan-labels
SECTOPTS	= -O3 -f bin ${WARN}
LDROPTS		= -f elf ${WARN} -D_KVERSION_=\"$(RELEASE)\"
LINKOPTS	= -Wl,-warn-common,-X,--oformat,binary,-e,loaderMain,-Ttext,0x00000000
STDDEPS		= loader.h Makefile
BOOTDEPS	= bootsect-diskparms.s bootsect-print.s \
		bootsect-error.s bootsect-read.s 
OBJS		= $(OBJDIR)/loaderMain.o \
		$(OBJDIR)/loaderA20.o \
		$(OBJDIR)/loaderVideo.o \
		$(OBJDIR)/loaderDetectHardware.o \
		$(OBJDIR)/loaderLoadFile.o \
		$(OBJDIR)/loaderLoadKernel.o \
		$(OBJDIR)/loaderDiskError.o \
		$(OBJDIR)/loaderPrintRoutines.o

all: target-dirs bootsectors ${LOADERBUILDDIR}/alldr

target-dirs:
	$(Q)mkdir -p $(OBJDIR) ${BOOTBUILDDIR} ${LOADERBUILDDIR}

bootsectors: \
${BOOTBUILDDIR}/mbr.simple \
${BOOTBUILDDIR}/mbr.bootmenu \
${BOOTBUILDDIR}/bootmenu \
${BOOTBUILDDIR}/bootsect-fat12 \
${BOOTBUILDDIR}/bootsect-fat16 \
${BOOTBUILDDIR}/bootsect-fat12_16-noboot \
${BOOTBUILDDIR}/bootsect-fat32 \


CMD_NASM = \
	$(Q)echo "NASM    "$(subst $(M_DIR_ROOT)/,,$@) && \
	$(NASM)

#
# MBRs and Boot sectors
#


${BOOTBUILDDIR}/mbr.simple: mbr-simple.s ${BOOTDEPS} ${STDDEPS}
	${CMD_NASM} ${SECTOPTS} $< -o $@

${BOOTBUILDDIR}/mbr.bootmenu: mbr-bootmenu.s ${BOOTDEPS} ${STDDEPS}
	${CMD_NASM} ${SECTOPTS} $< -o $@

${BOOTBUILDDIR}/bootmenu: bootmenu.s ${BOOTDEPS} ${STDDEPS}
	${CMD_NASM} ${SECTOPTS} $< -o $@

${BOOTBUILDDIR}/bootsect-fat12: bootsect-fat12_16.nasm
	${CMD_NASM} ${SECTOPTS} -DFAT12 $< -o $@

${BOOTBUILDDIR}/bootsect-fat16: bootsect-fat12_16.nasm
	${CMD_NASM} ${SECTOPTS} -DFAT16 $< -o $@

${BOOTBUILDDIR}/bootsect-fat12_16-noboot: bootsect-fat12_16.nasm
	${CMD_NASM} ${SECTOPTS} -DNOBOOT $< -o $@

${BOOTBUILDDIR}/bootsect-fat32: bootsect-fat32.nasm
	${CMD_NASM} ${SECTOPTS} $< -o $@

#
# Loader files
#

${LOADERBUILDDIR}/alldr: ${OBJS} ${STDDEPS}
	$(Q)echo "LD      "$(subst $(M_DIR_ROOT)/,,$@) && \
	${CC} -nodefaultlibs -nostartfiles ${OPT} ${LINKOPTS} ${OBJS} -o $@

$(OBJDIR)/%.o: %.s ${STDDEPS}
	${CMD_NASM} ${LDROPTS} $< -o $@

#
# Miscellany
#

clean:
	$(Q)rm -Rf *~ *.o core $(OBJDIR)
	$(Q)rm -Rf ${BOOTBUILDDIR} ${LOADERBUILDDIR}/alldr
